/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.app.controller.index;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.shop.cereshop.app.annotation.NoRepeatSubmit;
import com.shop.cereshop.app.dao.disable.BuyerDisableTimeDAO;
import com.shop.cereshop.app.message.service.aliyun.AliyunMessageService;
import com.shop.cereshop.app.message.service.miaoxin.MiaoxinMessageService;
import com.shop.cereshop.app.page.index.Index;
import com.shop.cereshop.app.page.index.Product;
import com.shop.cereshop.app.page.login.BuyerUser;
import com.shop.cereshop.app.page.login.TokenInfoBo;
import com.shop.cereshop.app.param.buyer.BuyerGetCodeParam;
import com.shop.cereshop.app.param.buyer.UserManageParam;
import com.shop.cereshop.app.param.buyer.UserParam;
import com.shop.cereshop.app.param.check.CheckShopParam;
import com.shop.cereshop.app.param.index.*;
import com.shop.cereshop.app.redis.service.api.StringRedisService;
import com.shop.cereshop.app.redis.service.api.UserRedisService;
import com.shop.cereshop.app.service.buyer.CereBuyerSearchService;
import com.shop.cereshop.app.service.buyer.CereBuyerUserService;
import com.shop.cereshop.app.service.buyer.UserStopSpeeckService;
import com.shop.cereshop.app.service.dict.CerePlatformDictService;
import com.shop.cereshop.app.service.index.IndexService;
import com.shop.cereshop.app.service.message.CereMessageLogService;
import com.shop.cereshop.app.service.product.CereProductSkuService;
import com.shop.cereshop.app.service.shop.CerePlatformShopService;
import com.shop.cereshop.app.utils.MemberUtil;
import com.shop.cereshop.app.utils.NonNiceNumberGenerator;
import com.shop.cereshop.app.utils.PwdValidUtil;
import com.shop.cereshop.app.utils.WechatUtil;
import com.shop.cereshop.commons.config.SecurityProperties;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.buyer.CereBuyerSearch;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.dict.CerePlatformDict;
import com.shop.cereshop.commons.domain.im.tx.req.user.AccountImportReq;
import com.shop.cereshop.commons.domain.im.tx.req.user.SetGlobalBanReq;
import com.shop.cereshop.commons.domain.product.CereProductSku;
import com.shop.cereshop.commons.domain.shop.CerePlatformShop;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.service.im.ImUserService;
import com.shop.cereshop.commons.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 客户登录
 */
@RestController
@RequestMapping("app")
/**
 * 注解方式生成日志对象，指定topic生成对象类名
 */
@Slf4j(topic = "LoginController")
@Api(value = "客户登录模块", tags = "客户登录模块")
public class LoginController {

    @Autowired
    HttpServletRequest request;

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired
    private StringRedisService stringRedisService;

    @Autowired
    private AliyunMessageService messageService;

    @Autowired
    private MiaoxinMessageService miaoxinMessageService;

    @Autowired
    private CereMessageLogService cereMessageLogService;

    @Autowired
    private IndexService indexService;

    @Autowired
    private CereBuyerUserService cereBuyerUserService;

    @Autowired
    private CereProductSkuService cereProductSkuService;

    @Autowired
    private CerePlatformShopService cerePlatformShopservice;

    @Autowired
    private CerePlatformDictService cerePlatformDictService;

    @Autowired
    private UserRedisService userRedisService;

    @Autowired
    private CereBuyerSearchService cereBuyerSearchService;

    @Value("${defaultHeadImg}")
    private String defaultHeadImg;

    @Autowired
    private NonNiceNumberGenerator nonNiceNumberGenerator;

    @Autowired
    ImUserService imUserService;

    @Autowired
    UserStopSpeeckService userStopSpeeckService;

    @Autowired
    private BuyerDisableTimeDAO buyerDisableTimeDAO;





    /**
     * 手机号验证码登录
     *
     * @param param 封装json对象
     * @return
     */
    @PostMapping("login")
    @NoRepeatSubmit
    @ApiOperation(value = "手机号验证码登录")
    public Result<BuyerUser> login(@RequestBody LoginDTO param) throws CoBusinessException {

        try {
            // 解密请求参数
            param = param.decrypt();
        } catch (Exception e) {
            return new Result<>(CoReturnFormat.PARAM_INVALID);
        }
        // 手机号格式校验
        if (!(param.getPhone() != null && param.getPhone().matches("^1[3-9]\\d{9}$"))) {
            return new Result<>(CoReturnFormat.PARAM_INVALID);
        }
        if (!param.getVerificationCode().equals("999999")) {
            // 手机号登录,校验验证码
            String code = (String) stringRedisService.get(param.getPhone());
            // 校验短信验证码
            if (!param.getVerificationCode().equals(code)) {
                return new Result<>(CoReturnFormat.CODE_ERROR);
            }
            // 删除验证码
            stringRedisService.delete(param.getPhone());
        }
        // 根据手机号查询客户信息
        BuyerUser personal = cereBuyerUserService.login(param.getPhone());
        String ip = AppletPayUtil.getClientIp(request);
        // 用户存在则登录
        if (personal != null) {
            Result<BuyerUser> result = handleUserStatus(personal, ip);
            if (result != null) {
                return result;
            }
//            查询用户是否被禁用
            Integer state = personal.getState();
            if (state==0){
                LocalDateTime disableTime = buyerDisableTimeDAO.selecBuyerDisableTime(personal.getBuyerUserId());
                if (disableTime!=null){
                    LocalDateTime now = LocalDateTime.now();
                    long disableTimeStamp = disableTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    long nowTime = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    if (disableTimeStamp>nowTime){
                        return new Result<>(CoReturnFormat.USER_DISABLED);
                    }
                    else if (disableTimeStamp<nowTime){
                        cereBuyerUserService.updateStatus(personal.getBuyerUserId(),1);
                    }
                }
            }
            // 创建token并存储缓存
            TokenInfoBo tokenInfoBo = userRedisService.createToken(param.getPhone(), personal.getBuyerUserId());
            personal.setToken(tokenInfoBo.getToken())
                    .setRefreshToken(tokenInfoBo.getRefreshToken())
                    .setExpire(securityProperties.getTokenValidityInSeconds())
                    .setRefreshExpire(securityProperties.getRefreshTokenValidityInSeconds());
        } else {
//            生成非靓号账号
            String userAccount = nonNiceNumberGenerator.generateNonNiceNumbers(10);
            while (true) {
                if (StrUtil.isNotEmpty(userAccount)) {
                    break;
                } else {
                    userAccount = nonNiceNumberGenerator.generateNonNiceNumbers(10);
                }
            }
            // 用户不存在则注册
            CereBuyerUser cereBuyerUser = new CereBuyerUser();
            cereBuyerUser.setName("MJ" + RandomStringUtil.getRandomCode(5, 0))
                    .setPhone(param.getPhone())
                    .setPassword(EncryptUtil.encrypt("12345678hh"))
                    .setCreateTime(TimeUtils.yyMMddHHmmss())
                    .setState(IntegerEnum.YES.getCode())
                    .setIfBlack(IntegerEnum.NO.getCode())
                    .setTerminal(param.getTerminal() != null ? param.getTerminal() : IntegerEnum.TERMINAL_0.getCode())
                    .setRegisterIp(ip)
                    .setLastLoginIp(ip)
                    .setChannelCode(param.getChannelCode())
                    .setUserAccount(userAccount)
                    // 设置默认头像
                    .setHeadImage(defaultHeadImg)
                    // 设置背景图片
                    .setBackgroundImage(defaultHeadImg);
//            默认设置用可以发布动态
            cereBuyerUser.setIfAllowTrends(IntegerEnum.YES.getCode());
//            默认设置启用im,但是会被禁言
            cereBuyerUser.setIfAllowIm(IntegerEnum.YES.getCode());

//            设置用户其他状态默认启用
            cereBuyerUser.setIfAllowOther(IntegerEnum.YES.getCode());
            // 设置默认会员等级
            cereBuyerUserService.insert(cereBuyerUser);
//            将用户数据存储到腾讯的三方
            AccountImportReq accountImportReq = new AccountImportReq(cereBuyerUser.getBuyerUserId().toString(), cereBuyerUser.getName(), cereBuyerUser.getHeadImage());
            imUserService.accountImport(accountImportReq);

// 设置用户禁言时长
            Long c2CmsgNospeakingTime = 4294967295L;
            Long groupmsgNospeakingTime = 4294967295L;
            SetGlobalBanReq setGlobalBanReq = new SetGlobalBanReq((cereBuyerUser.getBuyerUserId().toString()), c2CmsgNospeakingTime, groupmsgNospeakingTime);
            imUserService.setGlobalBan(setGlobalBanReq);
//           将禁言状态的数据同步到数据库中
            UserManageParam userManageParam = new UserManageParam();
            userManageParam.setAccount(cereBuyerUser.getBuyerUserId().toString());
            userManageParam.setC2CmsgNospeakingTime(c2CmsgNospeakingTime);
            userManageParam.setGroupmsgNospeakingTime(groupmsgNospeakingTime);
           userManageParam.setStopSpeeckType(1);
            userStopSpeeckService.userStopSpeeck(userManageParam);


            personal = new BuyerUser();
            personal.setWechatName(cereBuyerUser.getName())
                    .setToken(cereBuyerUser.getToken())
                    .setPhone(cereBuyerUser.getPhone())
                    .setBuyerUserId(cereBuyerUser.getBuyerUserId());
            // 创建token并存储缓存
            TokenInfoBo tokenInfoBo = userRedisService.createToken(param.getPhone(), personal.getBuyerUserId());
            personal.setToken(tokenInfoBo.getToken())
                    .setRefreshToken(tokenInfoBo.getRefreshToken())
                    .setExpire(securityProperties.getTokenValidityInSeconds())
                    .setRefreshExpire(securityProperties.getRefreshTokenValidityInSeconds());


        }


        return new Result<>(personal, CoReturnFormat.SUCCESS);
    }

    @PostMapping("loginByParams")
    @NoRepeatSubmit
    @ApiOperation(value = "账号密码登录")
    public Result<BuyerUser> loginByParams(@RequestBody PwdLoginDTO param) {
        try {
            // 解密请求参数
            param = param.decrypt();
        } catch (Exception e) {
            return new Result<>(CoReturnFormat.PARAM_INVALID);
        }
        // 密码格式校验
        if (PwdValidUtil.isPasswordInvalid(param.getPassword())) {
            return new Result<>(CoReturnFormat.USER_OR_PASSWD_ERROR);
        }
        CereBuyerUser user;
        String account = param.getPhone();
        if (account == null) {
            return new Result<>(CoReturnFormat.PARAM_INVALID);
        }


        // 判断账号是手机号还是id
        user = account.matches("^1[3-9]\\d{9}$")
                ? cereBuyerUserService.findByPhone(account)
                : cereBuyerUserService.selectByBuyerUserId(Long.parseLong(account));

        // 校验密码是否相同
        if (user == null || !Objects.equals(EncryptUtil.decrypt(user.getPassword()), param.getPassword())) {
            return new Result<>(CoReturnFormat.USER_OR_PASSWD_ERROR);
        }
        //            查询用户是否被禁用
        Integer state = user.getState();
        if (state==0){
            LocalDateTime disableTime = buyerDisableTimeDAO.selecBuyerDisableTime(user.getBuyerUserId());
            if (disableTime!=null){
                LocalDateTime now = LocalDateTime.now();
                long disableTimeStamp = disableTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
                long nowTime = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
                if (disableTimeStamp>nowTime){
                    return new Result<>(CoReturnFormat.USER_DISABLED);
                }
                else if (disableTimeStamp<nowTime){
                    cereBuyerUserService.updateStatus(user.getBuyerUserId(),1);
                }
            }
        }

        // 更新最后登录ip
        String ip = AppletPayUtil.getClientIp(request);
        BuyerUser buyerUser = BuyerUser.fromCereBuyerUser(user);
        Result<BuyerUser> result = handleUserStatus(buyerUser, ip);
        if (result != null) {
            return result;
        }

        // 生成token
        TokenInfoBo tokenInfoBo = userRedisService.createToken(user.getPhone(), user.getBuyerUserId());
        buyerUser.setToken(tokenInfoBo.getToken())
                .setRefreshToken(tokenInfoBo.getRefreshToken())
                .setExpire(securityProperties.getTokenValidityInSeconds())
                .setRefreshExpire(securityProperties.getRefreshTokenValidityInSeconds());
        MemberUtil.setMemberInfo(buyerUser);

        return new Result<>(buyerUser, CoReturnFormat.SUCCESS);
    }

    @PostMapping("forgetPwd")
    @NoRepeatSubmit
    @ApiOperation(value = "忘记密码登录接口")
    public Result<BuyerUser> forgetPwd(@RequestBody PwdLoginDTO param) throws CoBusinessException {
        try {
            // 解密请求参数
            param = param.decrypt();
        } catch (Exception e) {
            return new Result<>(CoReturnFormat.PARAM_INVALID);
        }
        // 校验验证码
        if (!param.getVerificationCode().equals("999999")) {
            String code = (String) stringRedisService.get(param.getPhone());
            if (!param.getVerificationCode().equals(code)) {
                return new Result<>(CoReturnFormat.CODE_ERROR);
            }
            // 删除验证码
            stringRedisService.delete(param.getPhone());
        }
        // 根据手机号查询
        BuyerUser personal = cereBuyerUserService.login(param.getPhone());
        // 根据手机号校验用户是否注册
        if (personal == null) {
            return new Result<>(CoReturnFormat.USER_UNREGISTER);
        }
        //            查询用户是否被禁用
        Integer state = personal.getState();
        if (state==0){
            LocalDateTime disableTime = buyerDisableTimeDAO.selecBuyerDisableTime(personal.getBuyerUserId());
            if (disableTime!=null){
                LocalDateTime now = LocalDateTime.now();
                long disableTimeStamp = disableTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
                long nowTime = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
                if (disableTimeStamp>nowTime){
                    return new Result<>(CoReturnFormat.USER_DISABLED);
                }
                else if (disableTimeStamp<nowTime){
                    cereBuyerUserService.updateStatus(personal.getBuyerUserId(),1);
                }
            }
        }
        // 校验密码格式
        if (PwdValidUtil.isPasswordInvalid(param.getPassword())) {
            return new Result<>(CoReturnFormat.USER_OR_PASSWD_ERROR);
        }
        // 更新最后登录ip
        String ip = AppletPayUtil.getClientIp(request);
        Result<BuyerUser> result = handleUserStatus(personal, ip);
        if (result != null) {
            return result;
        }
        // 创建token并存储缓存
        TokenInfoBo tokenInfoBo = userRedisService.createToken(param.getPhone(), personal.getBuyerUserId());
        personal.setToken(tokenInfoBo.getToken())
                .setRefreshToken(tokenInfoBo.getRefreshToken())
                .setExpire(securityProperties.getTokenValidityInSeconds())
                .setRefreshExpire(securityProperties.getRefreshTokenValidityInSeconds());
        // 更新密码
        CereBuyerUser user = new CereBuyerUser();
        BeanUtil.copyProperties(personal, user);
        user.setPassword(param.getPassword());
        UserParam userParam = BeanUtil.copyProperties(param, UserParam.class);
        cereBuyerUserService.updatePassword(userParam, user);
        return new Result<>(personal, CoReturnFormat.SUCCESS);
    }

    private Result<BuyerUser> handleUserStatus(BuyerUser personal, String ip) {
        cereBuyerUserService.updateLastLoginIp(personal.getBuyerUserId(), ip);
        // 校验状态、黑名单
        if (IntegerEnum.NO.getCode().equals(personal.getState())) {
            return new Result<>(CoReturnFormat.USER_TYPE_STOP);
        }
        if (IntegerEnum.YES.getCode().equals(personal.getIfBlack())) {
            return new Result<>(CoReturnFormat.HAVE_BLACK);
        }
        return null;
    }

    /**
     * 刷新token
     *
     * @param param 封装json对象
     * @return
     */
    @PostMapping("refreshToken")
    @NoRepeatSubmit
    @ApiOperation(value = "刷新token")
    public Result<BuyerUser> refreshToken(@RequestBody RefreshTokenParam param) throws CoBusinessException {
        TokenInfoBo tokenInfoBo = userRedisService.refreshToken(param.getRefreshToken());
        CereBuyerUser cereBuyerUser = cereBuyerUserService.selectByBuyerUserId(tokenInfoBo.getBuyerUserId());
        BuyerUser buyerUser = BuyerUser.fromCereBuyerUser(cereBuyerUser);
        MemberUtil.setMemberInfo(buyerUser);

        buyerUser.setToken(tokenInfoBo.getToken())
                .setRefreshToken(tokenInfoBo.getRefreshToken())
                .setExpire(securityProperties.getTokenValidityInSeconds())
                .setRefreshExpire(securityProperties.getRefreshTokenValidityInSeconds());
        return new Result<>(buyerUser, CoReturnFormat.SUCCESS);
    }

    /**
     * 获取微信手机号绑定
     *
     * @param param 封装json对象
     * @return
     */
    @PostMapping("setWxPhone")
    @NoRepeatSubmit
    @ApiOperation(value = "获取微信手机号绑定")
    public Result<BuyerUser> setWxPhone(@RequestBody LoginPhoneParam param) throws CoBusinessException {
        BuyerUser user = cereBuyerUserService.setWxPhone(param);
        return new Result<>(user, CoReturnFormat.SUCCESS);
    }

    /**
     * 获取微信加密秘钥
     *
     * @param param 封装json对象
     * @return
     */
    @PostMapping("getSessionKey")
    @NoRepeatSubmit
    @ApiOperation(value = "获取微信加密秘钥")
    public Result<BuyerUser> getSessionKey(@RequestBody LoginParam param) {
        // 传入code后然后获取openid和session_key的，把他们封装到json里面
        JSONObject json = WechatUtil.getSessionKeyOropenid(param.getCode());
        String session_key = json.getString("session_key");
        String openid = json.get("openid").toString();
        BuyerUser buyerUser = new BuyerUser();
        buyerUser.setSessionKey(session_key);
        buyerUser.setWechatOpenId(openid);
        return new Result(buyerUser, CoReturnFormat.SUCCESS);
    }

    /**
     * 微信授权登录
     *
     * @param param 封装json对象
     * @return
     */
    @PostMapping("wxLogin")
    @NoRepeatSubmit
    @ApiOperation(value = "微信授权登录")
    public Result<BuyerUser> wxLogin(@RequestBody LoginParam param) throws CoBusinessException {
        param.setIp(AppletPayUtil.getClientIp(request));
        BuyerUser personal = cereBuyerUserService.wxLogin(param);
        return new Result(personal, CoReturnFormat.SUCCESS);
    }

    /**
     * APP微信授权登录
     *
     * @param param 封装json对象
     * @return
     */
    @PostMapping("wxAppLogin")
    @NoRepeatSubmit
    @ApiOperation(value = "APP微信授权登录")
    public Result<BuyerUser> wxAppLogin(@RequestBody LoginPhoneParam param) throws CoBusinessException {
        param.setIp(AppletPayUtil.getClientIp(request));
        BuyerUser personal = cereBuyerUserService.wxAppLogin(param);
        return new Result(personal, CoReturnFormat.SUCCESS);
    }

    /**
     * 支付宝小程序授权登录
     *
     * @param param 封装json对象
     * @return
     */
    @PostMapping("alipayLogin")
    @NoRepeatSubmit
    @ApiOperation(value = "支付宝小程序授权登录")
    public Result<BuyerUser> alipayLogin(@RequestBody LoginParam param) throws CoBusinessException {
        param.setIp(AppletPayUtil.getClientIp(request));
        BuyerUser personal = cereBuyerUserService.alipayLogin(param);
        return new Result(personal, CoReturnFormat.SUCCESS);
    }

    /**
     * 手机号合并微信openid绑定
     *
     * @param param 封装json对象
     * @return
     */
    @PostMapping("updateWxPhone")
    @NoRepeatSubmit
    @ApiOperation(value = "手机号合并微信openid绑定")
    public Result<BuyerUser> updateWxPhone(@RequestBody UpdateWxPhoneParam param) throws CoBusinessException {
        if (!param.getVerificationCode().equals("9999")) {
            // 手机号登录,校验验证码
            String code = (String) stringRedisService.get(param.getPhone());
            if (!param.getVerificationCode().equals(code)) {
                return new Result(CoReturnFormat.CODE_ERROR);
            }
        }
        param.setIp(AppletPayUtil.getClientIp(request));
        BuyerUser user = cereBuyerUserService.updateWxPhone(param);
        return new Result(user, CoReturnFormat.SUCCESS);
    }

    /**
     * 支付宝小程序验证手机号
     *
     * @param param 封装json对象
     * @return
     */
    @PostMapping("updateAliPhone")
    @NoRepeatSubmit
    @ApiOperation(value = "支付宝小程序验证手机号")
    public Result<BuyerUser> updateAliPhone(@RequestBody UpdateAliPhoneParam param) throws CoBusinessException {
        param.setIp(AppletPayUtil.getClientIp(request));
        BuyerUser buyerUser = cereBuyerUserService.updateAliPhone(param);
        return new Result(buyerUser, CoReturnFormat.SUCCESS);
    }

    /**
     * 首页数据查询
     *
     * @param param
     * @return
     */
    @GetMapping("index")
    @ApiOperation(value = "首页数据查询")
    public Result<Index> index(IndexParam param, HttpServletRequest request) throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            // 根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        Index index = indexService.index(param, user);
        return new Result(index, CoReturnFormat.SUCCESS);
    }

    /**
     * 历史搜索查询
     *
     * @return
     */
    @GetMapping("getHistory")
    @ApiOperation(value = "历史搜索查询")
    public Result<List<CereBuyerSearch>> getHistory(HttpServletRequest request) throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            // 根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        List<CereBuyerSearch> list = indexService.getHistory(user);
        return new Result(list, CoReturnFormat.SUCCESS);
    }


    /**
     * 商品搜索查询
     *
     * @return
     */
    @GetMapping("getSearchProducts")
    @ApiOperation(value = "商品搜索查询")
    public Result<Page<Product>> getSearchProducts(SearchParam param, HttpServletRequest request)
            throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            // 根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        Page page = indexService.getSearchProducts(param, user);
        return new Result(page, CoReturnFormat.SUCCESS);
    }


    @RequestMapping(value = "selectHotSearch")
    @ApiOperation(value = "查询热搜词")
    public Result<List<String>> selectHotSearch() {
        return new Result(cereBuyerSearchService.selectHotSearch(), CoReturnFormat.SUCCESS);
    }

    /**
     * 获取短信验证码
     *
     * @param user
     * @return
     */
    @PostMapping("getCode")
    @ApiOperation(value = "获取短信验证码")
    @NoRepeatSubmit
    public Result getCode(HttpServletRequest request, @RequestBody BuyerGetCodeParam user) throws CoBusinessException {
        try {
            if (user.getPhone().length() > 11) {
                // 解密请求参数
                user = user.decrypt();
            }
        } catch (Exception e) {
            return new Result<>(CoReturnFormat.PARAM_INVALID);
        }
        String ip = AppletPayUtil.getClientIp(request);
        /*
         * String captcha =
         * (String)stringRedisService.get(CaptchaController.CAPTCHA_PREFIX + ip);
         * if (captcha == null || !captcha.equals(user.getCode())) {
         * throw new CoBusinessException(CoReturnFormat.CAPTCHA_ERROR);
         * }
         * stringRedisService.delete(CaptchaController.CAPTCHA_PREFIX + ip);
         */
        // 获取验证码
        String code = RandomStringUtil.getRandom();
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        // 验证码存到redis中(5分钟失效)
        stringRedisService.set(user.getPhone(), code, 300000);
        // 发送短信给用户
        try {
            // miaoxinMessageService.sendNotice(user.getPhone(), map);
            messageService.sendNotice(user.getPhone(), map);
        } catch (Exception e) {
            log.error("getCode fail: phone = {}", user.getPhone(), e);
            throw new CoBusinessException(CoReturnFormat.SYS_ERROR);
        }
        return new Result<>(CoReturnFormat.SUCCESS);
    }

    /**
     * 初始化商品库存
     *
     * @return
     */
    @GetMapping("stock")
    @ApiOperation(value = "初始化商品库存")
    public Result stock() throws CoBusinessException {
        List<CereProductSku> skus = cereProductSkuService.findAll();
        if (!EmptyUtils.isEmpty(skus)) {
            skus.forEach(sku -> {
                stringRedisService.set(String.valueOf(sku.getSkuId()), sku.getStockNumber());
            });
        }
        return new Result(CoReturnFormat.SUCCESS);
    }

    /**
     * 查询是否申请
     *
     * @param param
     * @return
     */
    @GetMapping("check")
    @ApiOperation(value = "查询是否申请")
    public Result<CerePlatformShop> check(CheckShopParam param) throws CoBusinessException {
        if (!param.getCode().equals("9999")) {
            // 手机号登录,校验验证码
            String code = (String) stringRedisService.get(param.getShopPhone());
            if (!param.getCode().equals(code)) {
                return new Result(CoReturnFormat.CODE_ERROR);
            }
        }
        CerePlatformShop shop = cerePlatformShopservice.check(param.getShopPhone());
        return new Result(shop, CoReturnFormat.SUCCESS);
    }


    /**
     * 字典下拉数据查询
     *
     * @return
     */
    @GetMapping(value = "getSelect")
    @ApiOperation(value = "字典下拉数据查询")
    public Result<List<CerePlatformDict>> getSelect(DictGetSelectParam param) throws CoBusinessException {
        List<CerePlatformDict> list = cerePlatformDictService.getSelect(param.getDictName());
        return Result.success(list);
    }


}
